﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sami_SMS_CRM_Business;

namespace Sami_SMS_CRM_UI
{
    public partial class mt_queue : BasePage
    {
        MtQueueRepository repository = new MtQueueRepository();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (!AllowManageAdvertisment())
            {
                RedirectToPage(AccessDeniedURL);
            }
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        void paging_PagingChange(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            pagingbottom.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            BindDataTable();
            pagingtop.Visible = (sender as UserControl.ucPaging).TotalPage > 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.Visible = (sender as UserControl.ucPaging).TotalPage > 1;
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDataAdvertismentAndCollection();
                PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
                pagingtop.CurrentPage = 1;
                pagingtop.PageSize = PageSize;
                pagingtop.PageDisplay = PageDisPlay;
                pagingbottom.CurrentPage = 1;
                pagingbottom.PageSize = PageSize;
                pagingbottom.PageDisplay = PageDisPlay;
                BindDataTable();
            }
        }

        private void LoadDataAdvertismentAndCollection()
        {
            if (ddlAdv.Items.Count == 0)
            {
                ddlAdv.Items.Add(new ListItem("-----------", ""));
                foreach (var item in new AdvertismentRepository().GetAll())
                {
                    ddlAdv.Items.Add(new ListItem(item.message, item.advertisment_id.ToString()));
                }
            }
            if (ddlCollection.Items.Count == 0)
            {
                ddlCollection.Items.Add(new ListItem("-----------", ""));
                foreach (var item in new CollectionRepository().GetAll())
                {
                    ddlCollection.Items.Add(new ListItem(item.collection_name, item.collection_id.ToString()));
                }
            }
        }

        private void BindDataTable()
        {
            try
            {
                int currentPage = pagingtop.CurrentPage;
                int pageSize = pagingtop.PageSize;
                int totalRecord = 0;
                string status = ddlStatus.SelectedValue;
                int? collection_id = string.IsNullOrEmpty(ddlCollection.SelectedValue) ?
                                        (int?)null : int.Parse(ddlCollection.SelectedValue);
                int? advertisment_id = string.IsNullOrEmpty(ddlAdv.SelectedValue) ?
                                        (int?)null : int.Parse(ddlAdv.SelectedValue);
                string subscriber = txtSubscriber.Text.Trim();
                DateTime? timeFrom = string.IsNullOrEmpty(txtFrom.Text) ?
                                        (DateTime?)null : Convert.ToDateTime(txtFrom.Text.Trim(), DateTimeFormat);
                DateTime? timeTo = string.IsNullOrEmpty(txtTo.Text) ?
                                        (DateTime?)null : Convert.ToDateTime(txtTo.Text.Trim(), DateTimeFormat);

                rptTable.DataSource = repository.GetAll(subscriber, advertisment_id, collection_id,
                                                        status, timeFrom, timeTo,
                                                        currentPage, pageSize, ref totalRecord);
                rptTable.DataBind();

                pagingtop.TotalRecord = totalRecord;
                pagingtop.Visible = pagingtop.TotalPage > 1;
                pagingbottom.TotalRecord = totalRecord;
                pagingbottom.Visible = pagingtop.TotalPage > 1;
                ltrTotalRow.Text = totalRecord.ToString();
            }
            catch (Exception e)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('" + Server.HtmlEncode(e.Message) + "');", true);
            }
        }

        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            pagingtop.PageSize = pagingbottom.PageSize = int.Parse((sender as DropDownList).SelectedValue);
            BindDataTable();
        }

        protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindDataTable();
        }

        protected void btnSearching_Click(object sender, EventArgs e)
        {
            pagingtop.CurrentPage = pagingbottom.CurrentPage = 1;
            BindDataTable();
        }
    }
}